package com.lazyfish.scss.servlet;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lazyfish.scss.bean.Course;
import com.lazyfish.scss.bean.User;
import com.lazyfish.scss.dao.CourseDAO;
import com.lazyfish.scss.util.GetRequestJsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class GetProductsInfo
 */
@WebServlet("/getAllCourse")
public class getAllCourseServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public getAllCourseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Map<String,String> map=new HashMap<String,String>();
        boolean flag=true;
        User user=new User();//用于获取seesion信息
        user = (User)request.getSession().getAttribute("user2");
        if(user==null){
            flag=false;
            map.put("status", "-1");
            map.put("msg", "未登录。");
        }
        if(flag){
            List<Course> list = new ArrayList<Course>();
            list = new CourseDAO().getAllCourse();
            PrintWriter out = response.getWriter();
            String ListUserJson = JSON.toJSONString(list);
            out.write(ListUserJson);
        }else{
            //输出json格式的map
            PrintWriter out = response.getWriter();
            String ListUserJson = JSON.toJSONString(map);
            out.write(ListUserJson);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
